package com.wang.example.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

public class HahaEncoder extends MessageToByteEncoder<String> {
    @Override
    protected void encode(ChannelHandlerContext ctx, String msg, ByteBuf out) throws Exception {
        out.writeInt(Constant.MAGIC);//写入协议开始协助标识
        out.writeInt(msg.length());//写入数据长度
        out.writeBytes(msg.getBytes(CharsetUtil.UTF_8));//消息字符串转为字节并写入
    }
}
